﻿using System.Windows;
using System.Windows.Controls;
using Brjnk.Common;
using Brjnk.GpxExplorer.ViewModel;
using Brjnk.GpxExplorer.Workspace;

namespace Brjnk.GpxExplorer.UI
{
    /// <summary>
    /// Interaction logic for GoToCoordinatesPanel.xaml
    /// </summary>
    public partial class GoToCoordinatesPanel : UserControl
    {
        public GoToCoordinatesPanel()
        {
            InitializeComponent();
            CopyToClipboardCommand = new GenericCommand(o => CopyToClipboard());
            CreateNamedPosition();
        }

        private void CopyToClipboard()
        {
            CoordinatesToClipboardWindow window = new CoordinatesToClipboardWindow(this.CoordinatesInternal);
            window.Show();
        }

        public bool ButtonGoTo
        {
            get { return (bool)GetValue(ButtonGoToProperty); }
            set { SetValue(ButtonGoToProperty, value); }
        }

        public static readonly DependencyProperty ButtonGoToProperty =
            DependencyProperty.Register("ButtonGoTo", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(false));

        public bool ButtonGoToBigZoom
        {
            get { return (bool)GetValue(ButtonGoToBigZoomProperty); }
            set { SetValue(ButtonGoToBigZoomProperty, value); }
        }

        public static readonly DependencyProperty ButtonGoToBigZoomProperty =
            DependencyProperty.Register("ButtonGoToBigZoom", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(false));

        public bool ButtonAddPushpinAndGoToBigZoom
        {
            get { return (bool)GetValue(ButtonAddPushpinAndGoToBigZoomProperty); }
            set { SetValue(ButtonAddPushpinAndGoToBigZoomProperty, value); }
        }

        public static readonly DependencyProperty ButtonAddPushpinAndGoToBigZoomProperty =
            DependencyProperty.Register("ButtonAddPushpinAndGoToBigZoom", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(true));

        public bool ButtonAddPushpinAndGoTo
        {
            get { return (bool)GetValue(ButtonAddPushpinAndGoToProperty); }
            set { SetValue(ButtonAddPushpinAndGoToProperty, value); }
        }

        public static readonly DependencyProperty ButtonAddPushpinAndGoToProperty =
            DependencyProperty.Register("ButtonAddPushpinAndGoTo", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(true));

        public bool ButtonAddPushpin
        {
            get { return (bool)GetValue(ButtonAddPushpinProperty); }
            set { SetValue(ButtonAddPushpinProperty, value); }
        }

        public static readonly DependencyProperty ButtonAddPushpinProperty =
            DependencyProperty.Register("ButtonAddPushpin", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(true));

        public bool ButtonCopyToClipboard
        {
            get { return (bool)GetValue(ButtonCopyToClipboardProperty); }
            set { SetValue(ButtonCopyToClipboardProperty, value); }
        }

        public static readonly DependencyProperty ButtonCopyToClipboardProperty =
            DependencyProperty.Register("ButtonCopyToClipboard", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(true));

        public bool ButtonCreateWaypoint
        {
            get { return (bool)GetValue(ButtonCreateWaypointProperty); }
            set { SetValue(ButtonCreateWaypointProperty, value); }
        }

        public static readonly DependencyProperty ButtonCreateWaypointProperty =
            DependencyProperty.Register("ButtonCreateWaypoint", typeof(bool), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(true));

        public string PushpinName
        {
            get { return (string)GetValue(PushpinNameProperty); }
            set { SetValue(PushpinNameProperty, value); }
        }

        public static readonly DependencyProperty PushpinNameProperty =
            DependencyProperty.Register("PushpinName", typeof(string), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(string.Empty, Changed));

        public GpsCoordinates GpsCoordinates
        {
            get { return (GpsCoordinates)GetValue(GpsPositionProperty); }
            set { SetValue(GpsPositionProperty, value); }
        }

        public static readonly DependencyProperty GpsPositionProperty =
            DependencyProperty.Register("GpsCoordinates", typeof(GpsCoordinates), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(default(GpsCoordinates), Changed));

        public IPoint PointObject
        {
            get { return (IPoint)GetValue(PointObjectProperty); }
            set { SetValue(PointObjectProperty, value); }
        }

        public static readonly DependencyProperty PointObjectProperty =
            DependencyProperty.Register("PointObject", typeof(IPoint), typeof(GoToCoordinatesPanel), new UIPropertyMetadata(null, PointObjectChanged));

        public GpsCoordinates CoordinatesInternal
        {
            get { return (GpsCoordinates)GetValue(CoordinatesInternalProperty); }
            protected set { SetValue(CoordinatesInternalPropertyKey, value); }
        }

        private static readonly DependencyPropertyKey CoordinatesInternalPropertyKey =
            DependencyProperty.RegisterReadOnly("CoordinatesInternal", typeof(GpsCoordinates), typeof(GoToCoordinatesPanel), new PropertyMetadata());

        public static readonly DependencyProperty CoordinatesInternalProperty = CoordinatesInternalPropertyKey.DependencyProperty;

        public bool IsMoveablePoint
        {
            get { return (bool)GetValue(IsMoveablePointProperty); }
            protected set { SetValue(IsMoveablePointPropertyKey, value); }
        }

        private static readonly DependencyPropertyKey IsMoveablePointPropertyKey =
            DependencyProperty.RegisterReadOnly("IsMoveablePoint", typeof(bool), typeof(GoToCoordinatesPanel), new PropertyMetadata());

        public static readonly DependencyProperty IsMoveablePointProperty = IsMoveablePointPropertyKey.DependencyProperty;

        private static void PointObjectChanged(object sender, DependencyPropertyChangedEventArgs e)
        {
            var iPoint = (IPoint)e.NewValue;
            var target = sender as GoToCoordinatesPanel;
            if (iPoint != null)
                target.SetValue(CoordinatesInternalPropertyKey, iPoint.PositionI);
            else
                target.SetValue(CoordinatesInternalPropertyKey, default(GpsCoordinates));
            target.IsMoveablePoint = e.NewValue is IMoveablePoint;
        }

        private static void Changed(object sender, DependencyPropertyChangedEventArgs e)
        {
            var target = sender as GoToCoordinatesPanel;
            target.CreateNamedPosition();
        }

        private void CreateNamedPosition()
        {
            SetCurrentValue(PointObjectProperty, new NamedPosition(PushpinName, GpsCoordinates));
            SetValue(CoordinatesInternalPropertyKey, this.GpsCoordinates);
        }

        public GenericCommand CopyToClipboardCommand { get; private set; }
    }
}